package com.hnxxxy.rewardmgr.mapper;

import com.hnxxxy.rewardmgr.bean.ExcelBean;
import com.hnxxxy.rewardmgr.bean.LuckDraw;
import com.hnxxxy.rewardmgr.bean.NumberConfig;
import com.hnxxxy.rewardmgr.bean.RewardLevel;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RewardMapper {
    @Select("select * from rwd_level_config")
    List<RewardLevel> loadRewardLevel();


    //"<if test='query != null and query != \" \" '>",
    //and remark like concat('%',#{remark},'%')
    @Select({"<script>",
            "select * from rwd_ratio_config",
            "<where> ",
            "<if test='name!=null and name!=\"\"'>",
            "and name like concat('%',#{name},'%')",
            "</if>",
            "<if test='numbers!=null and numbers!=\"\"'>",
            "and numbers= #{numbers}",
            "</if>",
            "<if test='rwd_state!=null and rwd_state!=\"\"'>",
            "and rwd_state= #{rwd_state}",
            "</if>",
            "</where>",
            "</script>"})
    List<NumberConfig> loadNumberConfig(@Param("name") String name, @Param("numbers") String numbers, @Param("rwd_state") String rwd_state);

    @Delete("delete from rwd_level_config where id=#{id}")
    int deleteRewardLevel(int id);

    @Update("update rwd_level_config a set a.name=#{name},a.is_ratio=#{is_ratio},a.role=#{role},a.ratio=#{ratio},a.img_url=#{img_url},a.img_show_state=#{img_show_state} where a.id=#{id}")
    int updateRewardLevel(RewardLevel rwd);

    @Insert("insert into  rwd_level_config(id,name,ratio,is_ratio,role,img_url,img_show_state) values(null,#{name},#{ratio} ,#{is_ratio} ,#{role} ,#{img_url},#{img_show_state})")
    int insertRewardLevel(RewardLevel rwd);

    @Insert("insert into  rwd_level_config(id,name,ratio,is_ratio,role,img_url ) values(#{id},#{name},#{ratio},#{is_ratio},#{role},#{img_url})")
    int insertRewardNotFond(RewardLevel rwd);

    @Select("select id,name,development,major,classes from rwd_nums_config")
    List<NumberConfig> numberConfig();

    @Select("select id,name,development,major,rwd_state,classes,role,phone from rwd_nums_config where role =#{role};")
    List<NumberConfig> numberConfig0(int role);

    @Select("select id,name,development,major,classes from rwd_nums_config where role =1;")
    List<NumberConfig> numberConfig1();

    @Select("select r.numbers,r.name,r.development,r.major,r.classes,r.rwd_state from rwd_ratio_config r where rwd_state = #{rwd_state}")
    @Results(value = {
            @Result(column = "numbers", property = "id")
    })
    List<NumberConfig> numberRatioConfig(int rwd_state);

    @Select("select l.id,l.name,l.ratio,l.is_ratio,l.role,l.img_url,l.img_show_state from rwd_level_config l where id = #{id}")
    RewardLevel levelConfigById(int id);

    @Delete("delete from rwd_nums_config  where id=#{id}")
    int deleteNumberConfig(String id);

    @Update("truncate table rwd_ratio_config")
    void truncateRatioConfig();

    @Update("update rwd_nums_config a set a.name=#{name},a.classes=#{classes},a.major=#{major},a.development=#{development} where a.id=#{id}")
    int updateNumberConfig(NumberConfig numberConfig);

    //123
    /*@Update("update rwd_nums_config a set a.rwd_state =#{rwd_state} where a.id=#{id}")
    int updateNumberConfig2(@Param("rwd_state") int rwd_state, @Param("id") String id);
*/
    @Insert("insert into  rwd_nums_config(id,name,development,major,classes,phone) values(#{id},#{name},#{development},#{major},#{classes},#{phone})")
    int insertNumberConfig(NumberConfig numberConfig);

    @Insert({"<script>" +
            "insert into rwd_ratio_config(numbers,name,development,major,classes,rwd_state,role,level_name) values" +
            "<foreach collection='list' item='x' index= 'index' separator =','>" +
            "(#{x.id},#{x.name},#{x.development},#{x.major},#{x.classes},#{x.rwd_state} ,#{x.role} ,#{x.level_name})" +
            "</foreach>" +
            "</script>"})
    int insertRatio(List<NumberConfig> numberConfig);


    @Select("select * from rwd_nums_config a where a.rwd_state is not null order by a.rwd_state asc")
    List<NumberConfig> showReward();

    @Delete(value = "delete from rwd_ratio_config where rwd_state=#{id}")
    int deleteRwdRationConfig(int id);

    @Insert({"<script>" +
            "replace into rwd_nums_config(id,name,development,major,classes,rwd_state,role,phone) values" +
            "<foreach collection='list' item='x' index= 'index' separator =','>" +
            "(#{x.id},#{x.name},#{x.development},#{x.major},#{x.classes},null,#{x.role},#{x.phone})" +
            "</foreach>" +
            "</script>"})
    int batchInsertNumberConfig(List<ExcelBean> list);

    @Select("select is_sms from sys_config")
    int sysSMSConfig();

    @Update(value = "truncate table rwd_nums_config;")
    int cleanNumberConfig();

    //open="(" close=")" separator="," item="id"
    @Delete({"<script>" +
            "delete from rwd_level_config where id in " +
            "<foreach collection='list' open='(' separator =',' close=')'  item='id' index= 'index'>" +
            "#{id}" +
            "</foreach>" +
            "</script>"})
    int batchDeleteLevel(List<Integer> list);

    @Select("select id,name,development,major,rwd_state,classes,role,phone from rwd_nums_config where role =#{role};")
    List<NumberConfig> numberConfigByRole(int role);
}